home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_08_07
/
8n07125a
< prev
next >
Wrap
Text File
|
1990-06-17
|
5KB
|
164 lines
1: /*
2: * MDBMAIN.C
3: *
4: * Program: Mini-Database
5: * Written by: Leor Zolman
6: * Module: Main Program Module
7: *
8: * Program Description:
9: * This system is an "introductory showcase" of
10: * C programming techniques for File I/O-related
11: * applications. Areas of focus include:
12: * Static and Dynamic Array Allocation
13: * Text-based and Binary-based Disk Data Storage
14: * Elementary user-interface and error-handling
15: *
16: * Compile & Link (Turbo C):
17: * tcc mdbmain.c mdbedit.c mdbutil.c
18: * {mdbftxt.c or mdbfbin.c}
19: */
20:
21: #include <stdio.h>
22: #include <stdlib.h>
23:
24: #define MAIN_MODULE 1 /* force data definitions */
25: #include "mdb.h"
26:
27: #define CREATE 1 /* Main menu action codes */
28: #define OPEN 2
29: #define EDIT 3
30: #define SAVE 4
31: #define BAKUP 5
32: #define CLOSE 6
33: #define ABANDON 7
34: #define QUIT 8
35:
36: static struct menu_item main_menu[] =
37: {
38: {CREATE, "Create New Database"},
39: {OPEN, "Select Existing Database to Work With"},
40: {EDIT, "Edit Database Records"},
41: {SAVE, "Write Database to Disk"},
42: {BAKUP, "Backup Database to Floppies"},
43: {CLOSE, "Close the Database"},
44: {ABANDON, "Abandon Changes to the Current Database"},
45: {QUIT, "Quit"},
46: {NULL} /* End of list */
47: };
48:
49:
50: main()
51: {
52: char db_name[150];
53: int db_active = FALSE; /* No Database open */
54: FILE *fp;
55: #if DYN_ARRAY
56: int array_size;
57: #endif
58:
59: while (1)
60: {
61: switch(do_menu(main_menu, "Main Menu"))
62: {
63: case CREATE:
64: if (db_active)
65: goto still_open;
66: printf("Name for new Database? ");
67: gets(db_name);
68: if ((fp = fopen(db_name,"r")) != NULL)
69: {
70: printf("That filename already exists.\n");
71: fclose(fp);
72: break;
73: }
74: #if DYN_ARRAY
75: array_size = MAX_TO_ADD * sizeof(struct record *);
76: if ((recs = malloc(array_size)) == NULL)
77: { /* allocate the memory */
78: printf("Couldn't allocate recs array.\n");
79: break;
80: }
81: max_recs = MAX_TO_ADD;
82: #else
83: max_recs = MAX_RECS;
84: #endif
85:
86: db_active = TRUE;
87: n_recs = 0;
88: printf("Database created; entering EDIT mode:\n");
89: /* After creating, fall through to EDIT */
90:
91: case EDIT:
92: if (!db_active)
93: goto inactive;
94: edit_db(db_name); /* Edit records in memory */
95: break;
96:
97: case OPEN:
98: if (db_active)
99: {
100: still_open: printf("Current Database still open.\n");
101: break;
102: }
103: printf("Database Name? ");
104: gets(db_name);
105: if ((n_recs = read_db(db_name)) != NULL)
106: {
107: printf("\nLoaded %d Record(s).\n", n_recs);
108: db_active = TRUE;
109: }
110:
111: edit_db(db_name);
112: break;
113:
114: case BAKUP:
115: if (!db_active)
116: goto inactive;
117: backup_db(); /* Perform backup */
118: break;
119:
120: case CLOSE:
121: if (!db_active)
122: goto inactive;
123: write_db(db_name); /* write to disk */
124: free_up();
125: db_active = FALSE;
126: break;
127:
128: case SAVE:
129: if (!db_active)
130: goto inactive;
131: write_db(db_name); /* write to disk */
132: break;
133:
134: case ABANDON:
135: if (!db_active)
136: {
137: inactive: printf("Please select a Database first!\n");
138: break;
139: }
140: free_up();
141: db_active = FALSE;
142: break;
143:
144: case QUIT:
145: if (db_active)
146: {
147: write_db(db_name); /* write to disk */
148: free_up();
149: }
150: exit(0);
151: }
152: }
153: }
154:
155: /*
156: * Function: backup_db
157: * Purpose: Backup current Database to floppies
158: * Parameters: None
159: * Return Value: None
160: */
161:
162: void backup_db() /* Backup module */
163: {}